﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace gradeManager
{
   
    public partial class LoginForm : Form
    {
        public static string StudentId;
        public LoginForm()
        {
            InitializeComponent();
        }

        private void 主界面_Load(object sender, EventArgs e)
        {
            label5.Text = "";
            MaximizeBox = false;        //关闭登录窗口的最大化功能
            Verification_Code.Text = VerificationCode.Code();                     //动态生成验证码
            button1.Enabled = false;//默认在不输入验证码时无法点击登录按钮

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //注册
            RegisterForm zc = new RegisterForm();
            zc.Show();
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtxuehao.Text.Trim() == "")
            {
                MessageBox.Show("用户名不能为空");
                txtxuehao.Focus();
                return;
            }
            else if (txtmima.Text.Trim() == "")
            {
                MessageBox.Show("密码不能为空!");
                txtmima.Focus();
                return;
            }

            if (txtvericode.Text != Verification_Code.Text)
            {
                label5.Text = "";
                return;
            }

            LinkSqLiteDB db = new LinkSqLiteDB();//创建数据库连接对象
            if (db.DBconn())
            {
                //数据库连接成功
                if (db.GetLoginData("select * from login_info where stuid='" + txtxuehao.Text + "'"))
                {
                    //用户存在
                    if (true)//账号已被审核允许登录
                    {
                        if (txtmima.Text.Equals(LoginInfo.password))//登录成功
                        {
                            MessageBox.Show("恭喜您已成功登录", "确定", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                            //主菜单 zhucaidan = new 主菜单();
                            // zhucaidan.ShowDialog();
                            db.DBclose();
                            this.DialogResult = DialogResult.OK;
                            return;
                        }
                        else
                        {  //密码错误

                            MessageBox.Show("登录失败,请重新输入");
                            txtxuehao.Text = "";
                            txtmima.Text = "";
                            return;
                        }
                    }
             
                }
                else
                {
                    MessageBox.Show("登录失败，用户名不存在!");
                    return;
                }
            }
            db.DBclose();//关闭数据库连接
            
        }


        private void txtvericode_TextChanged(object sender, EventArgs e)
        {
            if (txtvericode.Text != Verification_Code.Text)
            {
                label5.Text = "";
            }

            else
            {
                label5.Text = "";
                button1.Enabled = true;
            }
        }

        private void txtvericode_KeyUp(object sender, KeyEventArgs e)
        {
            if (txtvericode.Text != Verification_Code.Text)
            {
                label5.Text = "";
            }

            else
            {
                label5.Text = "";
                button1.Enabled = true;
            }
        }
        //验证用户登录

    }
    }



                

